//
//  RHMyRepairsController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/3.
//

#import "RHMyRepairsController.h"
#import "RHMyRepairsCell.h"
#import "RHEquipmentRepairVC.h"
#import "RHDeviceFaultModel.h"

@interface RHMyRepairsController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong)NSArray *deviceFaultArr;
@end

@implementation RHMyRepairsController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear: animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"我的维修".rh_localized];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self getWarmList];
    [self setupUI];
    
}

-(void)getWarmList
{
    [QMUITips showLoadingInView:self.view];
    [RHDeviceFaultModel startGetWithCompletionAPIKey:H_warrantyList argument:@{} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        if(resultModel.code == 200){
            self.deviceFaultArr = resultModel.data;
            [self.tableView endHeaderRefresh];
            [self.tableView reloadData];
        }
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}


- (void)setupUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHMyRepairsCell class] forCellReuseIdentifier:@"RHMyRepairsCell"];
    _tableView.ly_emptyView = [LYEmptyView emptyViewWithImage:[UIImage imageNamed:@"nodata_icon_1"] titleStr:@"没有数据".rh_localized detailStr:@""];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    kWeakSelf;
    if(_showRepairBtn){
        UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,40, 64)];
        [addButton setImage:[UIImage imageNamed:@"device_repair"] forState:UIControlStateNormal];
        [addButton setBackgroundColor:UIColor.clearColor];
        [addButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [addButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [addButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:addButton];
        self.navigationItem.rightBarButtonItem = rightBarBtn;
        
        [[addButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            RHEquipmentRepairVC *vc = [[RHEquipmentRepairVC alloc] init];
            vc.finishBlock = ^{
                [weakSelf getWarmList];
            };
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
    }
    
    [_tableView addHeaderRefresh:^{
            [weakSelf getWarmList];
    }];
}



#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.deviceFaultArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RHMyRepairsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHMyRepairsCell"];
    cell.faultModel = self.deviceFaultArr[indexPath.section];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


@end
